package frontend.user.controller;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import core.address.model.BasicAddress;
import core.discount.model.Coupon;
import core.profile.model.BasicCredential;
import core.profile.model.BasicProfile;
import core.util.HibernateCoreUtil;

public class GetDataByEmailId {
	
	public static BasicCredential getBasicCredentialByUserId(String userId)
	{
		BasicCredential bc = null ;
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try
		{

			transaction = session.beginTransaction();
			bc = (BasicCredential) session.createCriteria(BasicCredential.class).add(Restrictions.eq("userId", userId)).uniqueResult();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return bc ;
	}
	
	public static BasicAddress getBasicAddressByEmailId(String emailId)
	{
		BasicAddress ba=null;
		Transaction transaction =null;
		Session session= HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			ba = (BasicAddress) session.createCriteria(BasicAddress.class).add(Restrictions.eq("email", emailId)).uniqueResult();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return ba;
	}
	
	public static BasicProfile getBasicProfileByEmailId(String emailId)
	{
		BasicProfile bp=null;
		Transaction transaction =null;
		Session session= HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			bp = (BasicProfile) session.createCriteria(BasicProfile.class).add(Restrictions.eq("emailId", emailId)).uniqueResult();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return bp;
	}
	
	public static BasicProfile listProfileById(Long uuid) {
		BasicProfile baiscProfile = null;
		//Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try {
			//transaction = session.beginTransaction();
			baiscProfile = (BasicProfile)session.get(BasicProfile.class, uuid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return baiscProfile;
	}
	
	public static Coupon getCouponByCouponCode(String couponCode) {
		Coupon coupon = null;
		//Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try {
			//transaction = session.beginTransaction();
			coupon = (Coupon)session.get(Coupon.class, couponCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return coupon;
	}

}
